<?php
class Controller_test extends Controller_Abstract
{
    function actionpost(){
        $s=new cmshopapiclient_base();
        $s->verb='GetSessionID';
        $xmlArr=array(
				'RuName'=>$s->verb,
		);
        $r=$s->sendRequest($xmlArr);
        var_dump($r);die();
    }
    /***
     * 生成 client 应用 的 app_id 
     */         
    function actionAppclientSet(){
        $this->_view['a']=$a=array(
                'host'=>$this->_context->post('host'),
                'company'=>$this->_context->post('company'),
                'username'=>$this->_context->post('username'),
                'email'=>$this->_context->post('email'),
                'desc'=>$this->_context->post('desc'),
            );
        if($this->_context->isPost()){
           
            do{
            foreach(array('host','company','username','email') as $k){
                if(strlen($a[$k])){
                    $this->_view['error']['fill']=1;
                    break 2;
                }
            }
            $appclient=Appclient::find('host =? Or company=? Or username=? Or email =? ',
                $this->_context->post('host'),
                $this->_context->post('company'),
                $this->_context->post('username'),
                $this->_context->post('email'))->getOne();
            if($appclient->isNewRecord()){
                $a['app_name']=$this->_lengcode($this->_context->post('host'),5).'-'.
                    $this->_lengcode($this->_context->post('company'),6).'-'.
                    $this->_lengcode($this->_context->post('username'),7).'-'.
                    $this->_lengcode($this->_context->post('email'),8);
                $a['app_cert']=$this->_lengcode($this->_context->post('host','dmd5'),8).'-'.
                    $this->_lengcode($this->_context->post('company'),9,'2md5').'-'.
                    $this->_lengcode($this->_context->post('username'),10,'dmd5').'-'.
                    $this->_lengcode($this->_context->post('email'),10,'2md5');
                $a['status']=1;
                $appclient->changeProps($a);
                
                $appclient->save();
                $this->_view['error']['success']=1;
                
            }else{
                $this->_view['error']['exist']=1;
            }
            }while(0);
        }
    }
    
    /****
     * 指向 server 
     */         
    function actionAppclientLink(){
        $app=Appclient::find('app_id=1')->getOne();
        
        echo '/test/usertokenstep1/app_name/'.$app->app_name.'/app_cert/'.$app->app_cert;
        
        die();
    }
    /**
     * 取得  token
     */
    function actionUsertokenstep1(){
          // 1,app
        $this->_view['app']=$app=Appclient::find('app_name=? And app_cert=?',$this->_context->app_name,$this->_context->app_cert)->getOne();

        if($this->_context->isPost()){
        
        do{
            if($app->isNewRecord()){
                $this->_view['error']['app']=1;
                break;
            }
            //2 ,seller 
            $seller=Sellers::find('seller_name =? And password=?',$this->_context->seller_name,md5($this->_context->password))->getOne();
            if($seller->isNewRecord()){
                $this->_view['error']['seller']=1;
                break;
            }
            //3 ,Domain 
            $domain=Store_Domain::find('domain =? ',$this->_context->domain)->getOne();
            if($domain->isNewRecord()){
                $this->_view['error']['domain']=1;
                break;
            }
            //4 , store
            $store=Stores::find('store_id=?',$domain->store_id)->getOne();
            if($store->isNewRecord()){
                $this->_view['error']['store']=1;
                break;
            }
            //5,
            if($store->seller_id!=$seller->seller_id){
                $this->_view['error']['storeseller']=1;
                break;
            }
            // 6, 生成 token  
            $t=$this->_lengcode($app->app_id.'_'.$seller->seller_name.'_'.$seller->password,16,'time').'-'.
                $this->_lengcode($app->app_id.'_'.$domain->domain.'_'.$store->store_id,16,'time').'-'.
                $this->_lengcode($app->app_name,16,'time');
            //7,
            $token=Token::find('app_id=? And seller_id=? And store_id=? ',$app->app_id,$seller->seller_id,$store->store_id)->getOne();
            $token->token=$t;
            $token->hardexpirationtime=time()+15552000;
            if($token->isNewRecord()){
                $token->app_id=$app->app_id;
                $token->seller_id=$seller->seller_id;
                $token->store_id=$store->store_id;
            }
            $token->save();
            $this->_view['token']=$token;
            $this->_view['return']['token']=$token->token;
        }while(0);
        }
    }
    
    function action(){
        // 3ca660b02a4a9c95-70653ecdfa709bdd-ca02e04293b33e5b
    }
    
    function actionOrderstatus(){
        $o=Orders::find('order_id=1')->getOne();
//         $o->ship_street1='ship_street1';
//         $o->ship_street2='ship_street2';
//         $o->save();
        $o->ship_street1='11';
        $o->ship_street2='22';
        $o->save();
    }
    
    function actionpostgood(){
        $g=array(
            'sn'=>'testsn22',
            'price'=>'12',
            'market_price'=>'23',
            'weight'=>'23',
            'add_stock'=>'12',
            'category'=>'2',
            'tag'=>'',
            'image_url'=>'',
            'custom'=>'53',
            'language_desc'=>array(
                array(
                  'language_code'=>'en',
                  'title'=>'testgood',
                  'desc'=>'testgooddesc',
                  'property'=>'',
                  'introduction'=>'',
                ),
                array(
                  'language_code'=>'de',
                  'title'=>'testgood',
                  'desc'=>'testgooddesc',
                  'property'=>'',
                  'introduction'=>'',
                )
            ),
            
        );
        
        $additem=new cmshopapiclient_Additem();
        $additem->AuthToken='3ca660b02a4a9c95-70653ecdfa709bdd-ca02e04293b33e5b';
        $additem->Request($g);
        die('done');
    }
    /**
     * 接 收 order 
     */         
    function actionRequestorder(){
        Helper_Tlog::tofile('requestorder.log',file_get_contents('php://input'));
        die('dd');
    }
    
    function _lengcode($str,$len,$type='md5'){
        if($type=='md5'){
            $str2=md5($str);
        }elseif($type=='be'){
            $str2=$str.md5($str);
        }elseif($type=='ed'){
            $str2=md5($str).$str;
        }elseif($type=='time'){
            $str2=md5($str.'.'.time());
        }elseif($type=='dmd5'){
            $str2=md5(md5($str));
        }elseif($type=='2md5'){
            $str2=md5($str.':'.$str);
        }
        
        $start=strlen($str2)-$len;
        if($start<0) $start=0;
        $start=rand(0,$start);
        $str3=substr($str2,$start,$len);
        return $str3;
    }
    
    function actionsocket(){
        //$url="http://www.coomao.com/index.php?controller=services&action=CmshopNotificationReceiver";
        $url="http://work-right.coomao.com/index.php?controller=services&action=CmshopNotificationReceiver";
        //$url="http://www.17008.com/index.php?controller=default&action=login";
        $interface=new Helper_interface_base();
        $d=$interface->socket_post($url);
        echo "-------------------------\n";
        var_dump($interface->config);
        echo "-------------------------\n";
        var_dump($d);die();
    }
    
    function actionStoreInfo(){
        var_dump($_SERVER['HTTP_HOST']);
        var_dump(Q::ini('store'));
        var_dump($_SESSION['domain']);
        
        die();
    }
}

